package com.github.iot.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatEditText;

import com.github.iot.R;
import com.github.iot.dao.CommonDaoUtils;
import com.github.iot.dao.DaoUtilsStore;
import com.github.iot.entity.Door;
import com.github.iot.entity.DoorCommand;
import com.github.iot.utils.BarUtils;
import com.github.iot.utils.FastJsonUtils;
import com.github.iot.utils.KeyboardUtils;
import com.github.iot.utils.ToastUtils;
import com.github.iot.view.RxTitle;
import com.github.iot.view.dialogs.fragment.SimpleDialogFragment;
import com.github.iot.view.dialogs.iface.ISimpleDialogListener;

import java.util.UUID;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 管理柜门Activity
 *
 * @Author: 梁声洪
 * @Date: 2019/12/29 9:14
 * @Version: v1.0.0
 */
public class DoorActivity extends AppCompatActivity implements ISimpleDialogListener {

    private static final int REQUEST_SIMPLE_DIALOG = 1;

    @BindView(R.id.rx_title)
    RxTitle mTitle;

    @BindView(R.id.et_type)
    AppCompatEditText mType;

    @BindView(R.id.et_type_name)
    AppCompatEditText mTypeName;

    @BindView(R.id.et_address)
    AppCompatEditText mAddress;

    @BindView(R.id.et_sort)
    AppCompatEditText mSort;

    private static Integer tag;
    private static Long showcaseId;
    private static String doorType;
    private static Door door;

    private static CommonDaoUtils<Door> doorDaoUtils = DaoUtilsStore.getInstance().getDoorDaoUtils();
    private static CommonDaoUtils<DoorCommand> doorCommandDaoUtils = DaoUtilsStore.getInstance().getDoorCommandDaoUtils();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BarUtils.setStatusBarColor(this, R.color.colorPrimaryDark);
        setContentView(R.layout.activity_door);

        ButterKnife.bind(this);
        tag = getIntent().getIntExtra("TAG", 1);
        showcaseId = getIntent().getLongExtra("showcaseId", -1);
        doorType = getIntent().getStringExtra("doorType");

        if (tag == 2) {
            door = FastJsonUtils.strToBean(getIntent().getStringExtra("door"), Door.class);
        }

        initView();
        initData();
    }

    private void initView() {
        mTitle.setLeftFinish(this);
        mType.setText(doorType);
        mTypeName.setText(doorType);
        mType.setKeyListener(null);

        if (tag == 1) {
            mTitle.setTitle("添加" + doorType);
        } else if (tag == 2) {
            mTitle.setTitle(door.getName());
        }
    }

    private void initData() {

    }

    private void doorOperation() {
        String typeName = mTypeName.getText().toString();
        String address = mAddress.getText().toString();
        String sort = mSort.getText().toString();

        if (TextUtils.isEmpty(typeName)) {
            ToastUtils.warning("控制类型名称不能为空");
            return;
        }

        if (TextUtils.isEmpty(address)) {
            ToastUtils.warning("控制地址不能为空");
            return;
        }

        if (TextUtils.isEmpty(sort)) {
            ToastUtils.warning("排序不能为空");
            return;
        }

        if (1 == tag) {
            String uuid = UUID.randomUUID().toString();
            for (int i1 = 1; i1 <= 2; i1++) {
                Door door = new Door();
                door.setShowcaseId(showcaseId);
                door.setName(doorType + i1);
                door.setTypeName(typeName);
                door.setType(doorType);
                door.setUuid(uuid);
                door.setAddress(Integer.valueOf(address));

                if (1 == i1) {
                    door.setDownClose(203);
                    door.setUpClose(202);
                    door.setDownOpen(201);
                    door.setUpOpen(200);
                }

                if (2 == i1) {
                    door.setDownClose(207);
                    door.setUpClose(206);
                    door.setDownOpen(205);
                    door.setUpOpen(204);
                }

                door.setStatus(1);
                door.setSort(i1);
                doorDaoUtils.insert(door);
            }

            ToastUtils.success("操作成功");
            finish();
        } else {
            /*door.setName(name);
            door.setDownClose(Integer.valueOf(downClose));
            door.setUpClose(Integer.valueOf(upClose));
            door.setDownOpen(Integer.valueOf(downOpen));
            door.setUpOpen(Integer.valueOf(upOpen));
            door.setSort(Integer.valueOf(sort));

            boolean flag = doorDaoUtils.update(door);
            if (flag) {
                ToastUtils.success("操作成功");
                finish();
            } else {
                ToastUtils.error("操作失败");
            }*/
        }
    }

    @OnClick({R.id.btn_door})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_door:
                KeyboardUtils.hideSoftInput(DoorActivity.this);
                SimpleDialogFragment.createBuilder(DoorActivity.this, getSupportFragmentManager())
                        .setTitle("系统提示")
                        .setMessage("您确定要操作吗？")
                        .setPositiveButtonText("确定")
                        .setNegativeButtonText("取消")
                        .setRequestCode(REQUEST_SIMPLE_DIALOG)
                        .setCancelable(false)
                        .show();
                break;
            default:

                break;
        }
    }

    @Override
    public void onNegativeButtonClicked(int requestCode) {

    }

    @Override
    public void onNeutralButtonClicked(int requestCode) {

    }

    @Override
    public void onPositiveButtonClicked(int requestCode) {
        if (requestCode == REQUEST_SIMPLE_DIALOG) {
            doorOperation();
        }
    }

}
